#include "asyncimageview.h"
#include <QLabel>
#include <QImage>
#include <QPixmap>
#include <QNetworkReply>
#include <QFile>
#include <QDataStream>
#include <QDebug>
#include <QDir>
#include <QTextStream>
#include <QApplication>
#include "imageBll/loadimagebll.h"

static QNetworkAccessManager shareImageLoadAccessManager;

QString AsyncImageView::defaultImgCachePath(const QString &value)
{
    static QString patch = QString();
    if(!value.isEmpty()){
        patch = value;
    }
    return patch;
}

AsyncImageView::AsyncImageView(QWidget *parent) :
    QLabel(parent)
{
    setImgCachePath(AsyncImageView::defaultImgCachePath());
    _reply = 0;
    _netManager = &shareImageLoadAccessManager;
    connect(_netManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(loadImageFinish(QNetworkReply*)));
}
AsyncImageView::~AsyncImageView()
{
    LoadImageBLL *loadBll = LoadImageBLL::shareLoadImageBll();
    loadBll->removeLoad(this);
    disconnect(this);
    cancelLoad();

}

QString AsyncImageView::imgUrl() const
{
    return _imgUrl;
}
QString AsyncImageView::getImgCachePath() const
{
    return imgCachePath;
}

void AsyncImageView::setImgCachePath(const QString &value)
{
    imgCachePath = value;
}

void AsyncImageView::cancelLoad()
{
    if(_reply){
        _reply->abort();
//        _reply->deleteLater();
        delete _reply;
        _reply = 0;
    }
}
void AsyncImageView::loadImageOnDisk(EZThread*)
{

}

void AsyncImageView::loadImageFinish(LoadImageBLL *loadBll,LoadImageFinishEvent *event)
{

    if(event->_error ==0){
        QPixmap pixmap = event->_pixmap;
        setImageWithPixmap(pixmap);
    }else{
        //load on net
        QUrl url(_imgUrl);
//        const char *c = _imgUrl.toStdString().c_str();
        QNetworkRequest request(url);
        _reply =  _netManager->get(request);
    }
//    loadBll->removeLoad(this);
}

void AsyncImageView::onLoadImageOnDiskFinish()
{

}

int AsyncImageView::loadImageWithUrl(const QString &urlStr)
{
    if(urlStr.isEmpty()){
        return -1;
    }
    if( QString::compare(urlStr,_imgUrl,Qt::CaseInsensitive)==0 ){
        return 1;
    }
    _imgUrl = urlStr;

    cancelLoad();

    QString fileName = QString(_imgUrl).replace("/","_");
    QString filePath = imgCachePath + "/" + fileName;

//    EZGlobalThread *thread = EZGlobalThread::globalThread();
//    thread->addRunFunc();

    LoadImageBLL *bll = LoadImageBLL::shareLoadImageBll();
    bll->loadImageFromDisk(filePath,this);

    return 0;
}

void AsyncImageView::loadImageFinish(QNetworkReply *reply)
{
    if(_reply != reply){
        return;
    }
    QByteArray bytes = reply->readAll();
    QPixmap pixmap;
    bool isLoad = pixmap.loadFromData(bytes);
    if(isLoad ==false){

    }else{
        saveImage(pixmap);
        setImageWithPixmap(pixmap);
//        qDebug() << "load on net success";
    }

}
void AsyncImageView::saveImage(const QPixmap &pixmap)
{
    QString fileName = _imgUrl.replace("/","_");
    QString filePath = imgCachePath + "/" + fileName;
    QFile::remove(filePath);
    pixmap.save(filePath);
}

void AsyncImageView::setImageWithPixmap(const QPixmap &pixmap)
{
    if(pixmap.size().width() == size().width() && pixmap.size().height()==size().height()){
        setPixmap(pixmap);
//        qDebug() << "pixmap is equal size";
    }else{
        QPixmap scalePixmap = pixmap.scaled(size().width(),size().height());
        setPixmap(scalePixmap);
//        qDebug() << "pixmap is scale size" << size() << pixmap.size() ;
    }
}

